<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <context:component-scan base-package="ru.tbcarus.**.repository"/>

    <jdbc:initialize-database data-source="dataSource" enabled="${database.init}">
        <jdbc:script location="${jdbc.initLocation}"/>
        <jdbc:script encoding="utf-8" location="classpath:db/populateDB.sql"/>
    </jdbc:initialize-database>

    <beans>
        <!--
            For postgres driver logging
            It uses java.util.logging and logged via jul-to-slf4j bridge
            https://stackoverflow.com/a/27296470/548473
        -->
        <bean class="org.springframework.beans.factory.config.MethodInvokingBean">
            <property name="staticMethod" value="org.slf4j.bridge.SLF4JBridgeHandler.install"/>
        </bean>

        <context:property-placeholder location="classpath:db/postgres.properties" system-properties-mode="OVERRIDE"/>

        <bean id="dataSource"
              class="org.apache.tomcat.jdbc.pool.DataSource"
              p:driverClassName="org.postgresql.Driver"
              p:url="${database.url}"
              p:username="${database.username}"
              p:password="${database.password}"/>
    </beans>

    <beans>
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
              p:dataSource-ref="dataSource"
              p:packagesToScan="ru.tbcarus.**.model">
            <!--p:persistenceUnitName="persistenceUnit">-->

            <property name="jpaPropertyMap">
                <map>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).FORMAT_SQL}" value="${hibernate.format_sql}"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).USE_SQL_COMMENTS}" value="${hibernate.use_sql_comments}"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).JPA_PROXY_COMPLIANCE}" value="false"/>
                    <!--<entry key="#{T(org.hibernate.cfg.AvailableSettings).HBM2DDL_AUTO}" value="${hibernate.hbm2ddl.auto}"/>-->

                    <!--https://github.com/hibernate/hibernate-orm/blob/master/documentation/src/main/asciidoc/userguide/chapters/caching/Caching.adoc#caching-provider-jcache-->
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).CACHE_REGION_FACTORY}" value="org.hibernate.cache.jcache.internal.JCacheRegionFactory"/>
                    <entry key="#{T(org.hibernate.cache.jcache.ConfigSettings).PROVIDER}" value="org.ehcache.jsr107.EhcacheCachingProvider"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).USE_SECOND_LEVEL_CACHE}" value="true"/>
                    <entry key="#{T(org.hibernate.cfg.AvailableSettings).USE_QUERY_CACHE}" value="false"/> <!--default-->
                </map>
            </property>

            <property name="jpaVendorAdapter">
                <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="${jpa.showSql}">
                </bean>
            </property>
        </bean>

        <tx:annotation-driven/>

        <!-- Transaction manager for a single JPA EntityManagerFactory (alternative to JTA) -->
        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
              p:entityManagerFactory-ref="entityManagerFactory"/>

        <bean class="ru.tbcarus.topjava.repository.JpaUtil"/>
    </beans>

    <beans>
        <context:component-scan base-package="ru.tbcarus.**.repository"/>
        <jpa:repositories base-package="ru.tbcarus.**.repository"/>
    </beans>
</beans>